<?php
namespace Home\Controller;
use Think\Controller;
/**
* 用户相关控制器
*/
class OfoController extends Controller{

	public function index(){
		$user_id = get_user();
		$tui_id = M('user')->where('id='.$user_id)->getField('tui_id');
		if ($tui_id) 
			$data['tui_name'] =  M('user')->where('id='.$tui_id)->getField('username');
		else
			$data['tui_name'] = 'Next推荐';
		$data['tui_sum'] = M('user')->where('tui_id='.$user_id)->count();

		$dengji_id = $this->getDengji($data['tui_sum']);

		$deng = M('dengji')->find($dengji_id);
		$data['tui_dengji'] = 'LV'.$deng['grade'];
		$data['tui_val'] = $deng['val'];
		$data['jindutiao'] =round($data['tui_sum']/$deng['num']*100,2);//(int)($deng['grade'].'0');
		$data['photo_id'] = M('user')->where('id='.$user_id)->getField('photo_id');
		$data['shengji'] = $deng['num']-$data['tui_sum'];
		$data['fen_red'] = M('red_money')->where(array('user_id'=>$user_id,'type'=>3))->getField('sum(money)');
		$data['slide'] = M('config_img')->where('id=1')->getField('image');

		$result["result"]  = true;
		$result['message'] = "查询成功";
		$result['list'] = $data;
		$this->ajaxReturn($result);	
	}

	public function dengjiList(){
		$data = M('dengji')->select();

		foreach ($data as $k => $v) {
			$data[$k]['grade'] = 'LV'.$v['grade'];
			$data[$k]['red'] = '共享红包￥'.$v['red'];
		}
		// dump($data);exit();

		if ($data) 
		{	
			$result["result"]  = true;
			$result['message'] = "查询成功";
			$result['list'] = $data;
			$this->ajaxReturn($result);
		}else
		{	
			$result["result"]  = false;
			$result['message'] = "没有数据";
			$result['list'] = '';
			$this->ajaxReturn($result);
		}
	}

	public function getDengji($num){
		$data = M('dengji')->select();

		foreach ($data as $k => $v) {
			$nums[] = $v['num'];
			$ids[] = $v['id'];
		}

		if ($num<$nums[0]) {
			$id = $ids[0]; 
		}
		if ($num<$nums[1] && $num>$nums[0]) {
			$id = $ids[1]; 
		}
		if ($num<$nums[2] && $num>$nums[1]) {
			$id = $ids[2]; 
		}
		if ($num<$nums[3] && $num>$nums[2]) {
			$id = $ids[3]; 
		}
		if ($num<$nums[4] && $num>$nums[3]) {
			$id = $ids[4]; 
		}
		if ($num<$nums[5] && $num>$nums[4]) {
			$id = $ids[5]; 
		}
		if ($num<$nums[6] && $num>$nums[5]) {
			$id = $ids[6]; 
		}
		if ($num<$nums[7] && $num>$nums[6]) {
			$id = $ids[7]; 
		}
		if ($num<$nums[8] && $num>$nums[7]) {
			$id = $ids[8]; 
		}
		if ($num<$nums[9] && $num>$nums[8]) {
			$id = $ids[9]; 
		}
		if ($num<$nums[10] && $num>$nums[9]) {
			$id = $ids[10]; 
		}
		if ($num>$nums[10]) {
			$id = $ids[10]; 
		}

		return $id;
	}
	// 用户推荐的人列表
	public function tuiUser(){
		$user_id = get_user();
		$page_no = I('post.page_no') ? I('post.page_no') : 1;
		$data = M('user')->where('tui_id='.$user_id)->page($page_no,10)->field('id,username,photo_id,age,star,sex')->select();

		if ($data) 
		{	
			$result["result"]  = true;
			$result['message'] = "查询成功";
			$result['list'] = $data;
			$result['sum'] = M('user')->where('tui_id='.$user_id)->count();
			$this->ajaxReturn($result);
		}else
		{	
			$result["result"]  = false;
			$result['message'] = "没有数据";
			$result['list'] = '';
			$this->ajaxReturn($result);
		}
	}

	// 用户推荐的红包列表
	public function redMoney(){
		$user_id = get_user();
		$page_no = I('post.page_no') ? I('post.page_no') : 1;
		$data = M('red_money')
			->where(array('user_id'=>$user_id,'type'=>3))
			->page($page_no,10)
			->field('id,money,add_time')
			// ->join('left join zm_user b on a.user_id=b.id')
			->select();
		foreach ($data as $key => $value) 
		{
			$data[$key]['add_time'] = date("Y-m-d H:i",$value['add_time']);
		}
		if ($data) 
		{	
			$result["result"]  = true;
			$result['message'] = "查询成功";
			$result['list'] = $data;
			$result['sum'] = M('red_money')->where(array('user_id'=>$user_id,'type'=>3))->getField('sum(money)');
			$this->ajaxReturn($result);
		}else
		{	
			$result["result"]  = false;
			$result['message'] = "没有数据";
			$result['list'] = '';
			$result['sum'] = 0;
			$this->ajaxReturn($result);
		}
	}
	// 帮助文档
	public function article(){
		$data = M('article')->select();
		$result["result"]  = true;
		$result['message'] = "查询成功";
		$result['list'] = $data;
		$this->ajaxReturn($result);
	}

	// 意见反馈
	public function feedback(){
		$user_id = get_user();
		
		$content = I('post.content') ? I('post.content') : $this->ajaxReturn(is_not('content/必须'));
		
		$image = I('post.image') ? I('post.image') : $this->ajaxReturn(is_not('image/必须'));

		$image = A('Photo')->updata($image);
		
		$list = array(
			'user_id'    =>$user_id,
			'add_time'   =>time(),
			'image'      =>serialize($image),
			'content'    =>$content,
			);

		$data = M('feedback')->add($list);

		if ($data) 
		{	
			$result["result"] = true;
			$result['message'] = "提交成功";
			$result['list'] = $data;
			$this->ajaxReturn($result);
		}else
		{
			$result["result"] = false;
        	$result["message"] = '反馈失败';
        	$this->ajaxReturn($result);
		}

	}

	// 修改登录密码
	public function upPwd(){
		$user_id = get_user();
		$pwd = I('post.password') ? I('post.password') : $this->ajaxReturn(is_not('密码必须'));

		$pwd1 = I('post.password1') ? I('post.password1') : $this->ajaxReturn(is_not('新密码必须'));

		$pwd2 = I('post.password2') ? I('post.password2') : $this->ajaxReturn(is_not('重复新密码必须'));

		if ($pwd2 !=$pwd1) 
		{
			$this->ajaxReturn(is_not('两次输入的密码不一样'));
		}

		$paypwd = M('user')->where(array('id'=>$user_id))->getField('password');

		if ($paypwd!=md5($pwd)) 
		{
			$this->ajaxReturn(is_not('旧密码错误'));

		}

		$data = M('user')->where(array('id'=>$user_id))->save(array('password'=>md5($pwd1)));

		if ($data) 
		{	
			$result["result"] = true;
			$result['message'] = "修改成功";
			$this->ajaxReturn($result);
		}else
		{
			$this->ajaxReturn(is_not('修改失败'));
		}

	}

	// 我的电话
	public function getMoblie(){
		$id = get_user();

		$moblie['phone'] = M('user')->where('id='.$id)->getField('phone');

		if ($moblie) 
		{	
			$result["result"] = true;
			$result['message'] = "查询成功";
			$result['list'] = $moblie;
			$this->ajaxReturn($result);
		}else
		{
			$result["result"] = false;
        	$result["message"] = '查询失败';
        	$this->ajaxReturn($result);
		}
	}


}